import { ShopCart, deletes, isChecked } from "@/API/index"
const actions = {
    //购物车获取数据操作
    async ShopCartacti({ commit }) {
        let Shopcode = await ShopCart();
        if (Shopcode.code == 200) {
            commit("ShopMuta", Shopcode.data)
        }
    },
    //删除购物车操作网络请求
    async Deletes({ commit }, skuId) {
        let dele = await deletes(skuId);
        if (dele.code == 200) {
            return true
        }
        else {
            return Promise.reject(new Error('faile'));
        }
    },
    //切换前面选中情况（修改某一个产品的选中状态）
    async ChkList({ commit }, { skuId, isCheckeds }) {
        const Checked = await isChecked(skuId, isCheckeds)
        if (Checked.code == 200) {
            return true;
        }
        else {
            return Promise.reject(new Error('faile'));
        }
    },
    //删除选中的的函数
    deleteAllChecked({ dispatch, getters }) {
        // store这个就是一个小仓库
        //{ getters }这个就是找这两个模块
        // 之后就是找到了所有的商品   getters.cartInfo.cartInfoList
        var PromiseAll = [];
        getters.cartInfo.cartInfoList.forEach((item) => {
            if (item.isChecked) {
                let Promise = dispatch("Deletes", item.skuId);
                PromiseAll.push(Promise)
            }
        })
        return Promise.all(PromiseAll);
    },
    //全选按钮的函数
    checkboxAll({ dispatch, getters }, isCheckeds) {
        var checkboxAlls = []
        getters.cartInfo.cartInfoList.forEach((item) => {
            if (item.isChecked != isCheckeds) {
                let chekbox = dispatch("ChkList", { skuId: item.skuId, isCheckeds: isCheckeds })
                checkboxAlls.push(chekbox);
            }
        })
        return Promise.all(checkboxAlls);
    }

}
const mutations = {
    ShopMuta(state, shop) {
        return state.shopdata = shop;
    }
}
const state = {
    shopdata: []
}
const getters = {
    cartInfo(state) {
        return state.shopdata[0] || {};

    }
}
export default {
    actions,
    mutations,
    state,
    getters
}



